// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.weather.fog;

import org.lwjgl.opengl.ARBShaderObjects;
import zombie.core.SpriteRenderer;
import zombie.core.opengl.RenderThread;
import zombie.core.opengl.ShaderProgram;

public class FogShader {
    public static final FogShader instance = new FogShader();
    private ShaderProgram shaderProgram;
    private int noiseTexture;
    private int screenInfo;
    private int textureInfo;
    private int rectangleInfo;
    private int worldOffset;
    private int scalingInfo;
    private int colorInfo;
    private int paramInfo;
    private int cameraInfo;

    public void initShader() {
        this.shaderProgram = ShaderProgram.createShaderProgram("fog", false, true);
        if (this.shaderProgram.isCompiled()) {
            this.noiseTexture = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "NoiseTexture");
            this.screenInfo = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "screenInfo");
            this.textureInfo = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "textureInfo");
            this.rectangleInfo = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "rectangleInfo");
            this.scalingInfo = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "scalingInfo");
            this.colorInfo = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "colorInfo");
            this.worldOffset = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "worldOffset");
            this.paramInfo = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "paramInfo");
            this.cameraInfo = ARBShaderObjects.glGetUniformLocationARB(this.shaderProgram.getShaderID(), "cameraInfo");
            ARBShaderObjects.glUseProgramObjectARB(this.shaderProgram.getShaderID());
            ARBShaderObjects.glUseProgramObjectARB(0);
        }
    }

    public void setScreenInfo(float width, float height, float zoom, float secondLayerAlpha) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.screenInfo, width, height, zoom, secondLayerAlpha);
    }

    public void setTextureInfo(float u1, float u2, float alpha, float u3) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.textureInfo, u1, u2, alpha, u3);
    }

    public void setRectangleInfo(float x, float y, float w, float h) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.rectangleInfo, x, y, w, h);
    }

    public void setScalingInfo(float a, float b, float c, float d) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.scalingInfo, a, b, c, d);
    }

    public void setColorInfo(float r, float g, float b, float a) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.colorInfo, r, g, b, a);
    }

    public void setWorldOffset(float x, float y, float z, float u) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.worldOffset, x, y, z, u);
    }

    public void setParamInfo(float x, float y, float z, float w) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.paramInfo, x, y, z, w);
    }

    public void setCameraInfo(float x, float y, float z, float w) {
        SpriteRenderer.instance.ShaderUpdate4f(this.shaderProgram.getShaderID(), this.cameraInfo, x, y, z, w);
    }

    public boolean StartShader() {
        if (this.shaderProgram == null) {
            RenderThread.invokeOnRenderContext(this::initShader);
        }

        if (this.shaderProgram.isCompiled()) {
            zombie.IndieGL.StartShader(this.shaderProgram.getShaderID(), 0);
            return true;
        } else {
            return false;
        }
    }

    protected void reloadShader() {
        if (this.shaderProgram != null) {
            this.shaderProgram = null;
        }
    }
}
